       subroutine run_rhf(icore, isympopa, isympopb, 
     &                         isymvrta, isymvrtb,
     &                         isymmo, nvmax, nomax, nmo)
c     
      use mod_symm
      use mod_iop
      use mod_orbit
      use mod_size
      use mod_ioff
      implicit none
      integer*8, intent(in) :: nmo, nvmax, nomax, isymmo(nmo, 8)
      integer*8, intent(in) :: isymvrta(nvmax,8), isymvrtb(nvmax,8)
      integer*8, intent(in) :: isympopa(nomax,8), isympopb(nomax,8)
      real*8, intent(in) :: icore(*)
      double precision dtmp
c
      integer*8 maxcor, iintfp, iflags
c
      double precision eccsd, escf, scalfact, tmp
      double precision, allocatable::ea(:), eb(:)
c
      integer*8 :: ncycle
c
      integer*8:: lth, nbasis, nsol, nbas2, no2, nv2, nvo, no4, nv2o2
      integer*8:: iread, isox, isoy, isoz, itmp1, itmp2, imoc, norbs
      integer*8:: iaopop(8), irrep, i, j, nsize, idx, ioffa
      integer*8:: irrepab, irrepcd, irrepc, irrepd
c            
      integer*8:: iijkl, iiabj, iabij, iijka, naux
      integer*8:: nv3, nv3o, i0, i1, i2, i3, iend, nvt
      integer*8:: icontl, nabcdab, numab, numcd
      integer*8:: maxcor1, istat, irrepmo(nmo), ioffmo(nmo)
      integer*8:: c, d, numc, numd, irrepci, numi, irrepi
      integer*8:: numab1, numab2, numab3, numcd1, numcd2, numcd3
      integer*8:: vstart, nfirst, nread, nleft, idx2, nsize2
      double precision tt1, tt2,rt1, rt2, rt3, rt4, rt5, rt6 

      real*8, allocatable :: fijaar(:,:), fijaai(:,:)
      real*8, allocatable :: fijabr(:,:), fijabi(:,:)
      real*8, allocatable :: fiaaar(:,:), fiaaai(:,:)
      real*8, allocatable :: fiaabr(:,:), fiaabi(:,:)
      real*8, allocatable :: fabaar(:,:), fabaai(:,:)
      real*8, allocatable :: fababr(:,:), fababi(:,:)
      real*8, allocatable :: fijr(:,:), fiji(:,:)
      real*8, allocatable :: fiar(:,:), fiai(:,:)
      real*8, allocatable :: fabr(:,:), fabi(:,:)
      real*8, allocatable :: iaam(:), ibbm(:)
      real*8, allocatable :: ea_resort(:), eb_resort(:)
c
      character*12 string2, sfname
      logical :: lsoineom, lscalar, lbdf, lscalsoc
c     
      integer*8 memsize, rec_char, n
      integer*8 fcstar, nbas, nocca, noccb, nirreptmp
      integer*8, external :: idsymsz, irpdso, isymoffso, idsymsoc
      double precision, external :: dnrm2, nrm2so
      idx = 0  
      do n = 1, nmo 
         do irrep = 1, nirrep
         do i = 1, pop(irrep,1)+vrt(irrep,1)
            if(isymmo(i, irrep)==n) then 
               idx = idx + 1
               irrepmo(n) = irrep
               ioffmo(n) = i 
            endif
         enddo
         enddo
      enddo
!-------for ABAB
3000  call clmoioso0(1, icore, pop(1,1), pop(1,1), 1, 
     &                        pop(1,1), vrt(1,1), 1, 'IJKAAB__')
      call clmoioso0(1, icore, pop(1,1), vrt(1,1), 1, 
     &                        pop(1,1), pop(1,1), 1, 'KAIJAB__')
      call clmoioso0(1, icore, vrt(1,1), pop(1,2), 1, 
     &                       vrt(1,1), pop(1,2), 1, 'BIAJABAB')
      call clmoioso0(1, icore, vrt(1,2), pop(1,1), 1, 
     &                       vrt(1,2), pop(1,1), 1, 'BIAJAAAA')
      call clmoioso0(1, icore, vrt(1,1), pop(1,1), 1, 
     &                       vrt(1,2), pop(1,2), 1, 'BIAJAABB')
      call clmoioso0(1, icore, vrt(1,1), vrt(1,2), 1, 
     &                      pop(1,1), pop(1,2), 1, 'ABIJAB__')
      call clmoioso0(1, icore, pop(1,1), pop(1,2), 1, 
     &                      pop(1,1), pop(1,2), 1, 'IJKLAB__')
      if(eri_level==0.or.eri_level==1) then 
      call clmoioso0(1, icore, vrt(1,1), vrt(1,2), 1, 
     &                     vrt(1,1), pop(1,2), 1, 'ABCIAB__')
      endif
      if(eri_level==0) then 
      call clmoioso0(1, icore, vrt(1,1), vrt(1,2), 2, 
     &                     vrt(1,1), vrt(1,2), 2, 'ABCDAB2_')
      call clmoioso0(1, icore, vrt(1,1), vrt(1,2), 0, 
     &                     vrt(1,1), vrt(1,2), 0, 'ABCDAA__')
      endif
      if(eri_cfour==1.and.eri_level==0) then 
      call clmoioso0(1, icore, vrt(1,1), vrt(1,2), 1, 
     &                     vrt(1,1), vrt(1,2), 1, 'ABCDAB__')
      endif
    
      call cpu_time(tt1)
      if(ldebug) then 
         write(6,*)'finished clmoioso'
      endif
      i0 = 1
      i1 = i0 + nmo**3
      i2 = i1 + nmo**3
!      write(6,*)'pop', pop(:,1), pop(:,2)
      if(eri_level==0) then 
        lth = nmo
      elseif(eri_level==1.or.eri_level==2) then 
        lth = noa
      else 
        write(6,*)'error in eri_level'
        stop
      endif 
      do i = 1, lth
          if(ldebug) then 
!             write(6,*)'i', i, nmo
          endif
          call pyscfio_split('ab_c', 'eri_mo', icore(i1),
     &                 (i-1)*nmo+1, nmo, nmo*(nmo+1)/2) 
          nirreptmp = nirrep
          nirrep = 1 
          call iexpso(icore(i1), icore(i0), nmo, nmo, 2, 1,
     &                  nmo, zorb, 1, 0, 1)
          nirrep = nirreptmp
          call mopy2mo_rhf(icore(i0), icore(i1), nmo, 
     &                  ioffmo(i), irrepmo(i), isymmo, isympopa,
     &                  isympopb, isymvrta, isymvrtb, nomax, nvmax)
      enddo
      if(ldebug) then 
         write(6,*)'finished mopy2mo!'
      endif

      call getallso(icore, 1, 'KAIJAB__')
      nsize = idsymsoc(1, popa, popb, 1, popa, vrtb, 1)
      call itranspso(icore, icore(nsize+1), popa, vrta, 1, popa, popa,1, 1) 
      call putallso(icore(nsize+1), 1, 'IJKAAB__')
      if(ldebug) then 
          call getallso(icore, 1, 'IJKLAB__')
          nsize = idsymsoc(1, popa, popb, 1, popa, popb, 1)
          write(6,*)'norm of IJKLAB  ',nsize, dnrm2(nsize, icore, 1)
    
          call getallso(icore, 1, 'IJKAAB__')
          nsize = idsymsoc(1, popa, popb, 1, popa, vrtb, 1)
          write(6,*)'norm of IJKAAB  ',nsize, dnrm2(nsize, icore, 1)
    
          call getallso(icore, 1, 'ABIJAB__')
          nsize = idsymsoc(1, vrta,vrtb,1,popa,popb, 1)
          write(6,*)'norm of ABIJAB  ',nsize, dnrm2(nsize, icore, 1)
    
          call getallso(icore, 1, 'BIAJAABB')
          nsize = idsymsoc(1, vrta,popa,1,vrtb,popb, 1)
          write(6,*)'norm of BIAJAABB',nsize, dnrm2(nsize, icore, 1)
    
          call getallso(icore, 1, 'BIAJABAB')
          nsize = idsymsoc(1,vrta,popb,1, vrta, popb,1)
          write(6,*)'norm of BIAJABAB',nsize, dnrm2(nsize, icore, 1)
    
          call getallso(icore, 1, 'BIAJAAAA')
          nsize = idsymsoc(1,vrtb,popa, 1,vrtb, popa,1)
          write(6,*)'norm of BIAJAAAA',nsize, dnrm2(nsize, icore, 1)
          if(eri_level==0.or.eri_level==1) then 
             call getallso(icore, 1, 'ABCIAB__')
             nsize = idsymsoc(1,vrta,vrtb,1 ,vrta,popb, 1)
             write(6,*)'norm of ABCIAB  ',nsize, dnrm2(nsize, icore, 1)
          endif
       endif
!c---------
      if(ldebug) then 
          write(6,*)'finished cc_inter'
      endif
      return
      end



